﻿@using DevOpsLib
@model AgentMatrixViewModel

@{
    ViewData["Title"] = "Agent Matrix";

    const int IoTEdgeAgentQueueId = 379;
    string IoTEdgeAgentUrl = "https://dev.azure.com/msazure/One/_settings/agentqueues?agentId={0}&queueId={1}";
}

<div class="text-center">
    <h1 class="display-4">Agent matrix</h1>

    <table class="agent-matrix">
        <thead>
        <tr>
            <td>&nbsp;</td>
            @foreach (AgentDemandSet column in Model.AgentTable.Columns) {
                <td>
                    <b>@column.Name</b>
                    @if (column.Capabilities.Any()) {
                        <br/>
                        <i style="font-size: 0.75em">
                            &#123;
                            @for (int i = 0; i < column.Capabilities.Count; i++) {
                                if (i != 0) {
                                    <br/>
                                }
                                @(column.Capabilities[i].ToString())
                            }
                            &#125;
                        </i>
                    }
                </td>
            }
        </tr>
        </thead>
        @foreach (AgentDemandSet row in Model.AgentTable.Rows) {
            <tr>
                <td>
                    <b>@row.Name</b>
                    @if (row.Capabilities.Any()) {
                        <br/>
                        <i style="font-size: 0.75em">
                            &#123;
                            @for (int i = 0; i < row.Capabilities.Count; i++) {
                                if (i != 0) {
                                    <br/>
                                }
                                @(row.Capabilities[i].ToString())
                            }
                            &#125;
                        </i>
                    }
                </td>
                @foreach (AgentDemandSet column in Model.AgentTable.Columns) {
                    HashSet<IoTEdgeAgent> agents = Model.AgentTable.Matrix[row][column];
                    <td>
                        @foreach (IoTEdgeAgent agent in agents.OrderBy(a => a.GetAgentGroup())) {
                            string agentLabel = agent.Name;
                            string group = agent.GetAgentGroup();
                            if (!string.IsNullOrWhiteSpace(group)) {
                                agentLabel += "[" + group + "]";
                            }

                            <div class="agent">
                                <span class="@(agent.IsAvailable ? "agent-available" : "agent-unavailable")">&#10625;</span>
                                <a href="@(string.Format(IoTEdgeAgentUrl, agent.Id, IoTEdgeAgentQueueId))">@agentLabel</a>
                            </div>
                        }
                        &nbsp;
                    </td>
                }
            </tr>
        }
        <tr>
            <td colspan="@(Model.AgentTable.Columns.Count + 1)" style="text-align: left">
                Note: &lt;agent-name&gt;[agent-group]
            </td>
        </tr>
        <tr>
            <td>
                <b>@Model.ImageBuild.Group.Name</b>
                <br/>
                <i style="font-size: 0.75em">
                    &#123;
                    @for (int i = 0; i < Model.ImageBuild.Group.Capabilities.Count; i++) {
                        if (i != 0) {
                            <br/>
                        }
                        @(Model.ImageBuild.Group.Capabilities[i].ToString())
                    }
                    &#125;
                </i>
            </td>
            <td colspan="@(Model.AgentTable.Columns.Count)">
                @{
                    foreach (IoTEdgeAgent agent in Model.ImageBuild.Agents.OrderBy(a => a.Name)) {
                        <span class="agent">
                            <span class="@(agent.IsAvailable ? "agent-available" : "agent-unavailable")">&#10625;</span>
                            <a href="@(string.Format(IoTEdgeAgentUrl, agent.Id, IoTEdgeAgentQueueId))">@agent.Name</a>
                        </span>
                    }
                }
            </td>
        </tr>
        <tr>
            <td colspan="@(Model.AgentTable.Columns.Count + 1)" class="unmatched-agents">
                <h4>
                    Other agents
                </h4>
                @{
                    foreach (IoTEdgeAgent agent in Model.UnmatchedAgents.OrderBy(a => a.Name)) {
                        <span class="agent">
                            <span class="@(agent.IsAvailable ? "agent-available" : "agent-unavailable")">&#10625;</span>
                            <a href="@(string.Format(IoTEdgeAgentUrl, agent.Id, IoTEdgeAgentQueueId))">@agent.Name</a>
                        </span>
                    }
                }
            </td>
        </tr>
    </table>
</div>